package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "房间信息管理")
@RestController
@RequestMapping("/admin/room")
@RequiredArgsConstructor
public class RoomController {

    private final RoomInfoService roomInfoService;

    private final GraphInfoService graphInfoService;

    private final RoomAttrValueService roomAttrValueService;

    private final RoomFacilityService roomFacilityService;

    private final RoomLabelService roomLabelService;

    private final RoomPaymentTypeService roomPaymentTypeService;

    private final RoomLeaseTermService roomLeaseTermService;

    @Operation(summary = "保存或更新房间信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) {
        if (roomSubmitVo.getId() != null){
            // 删除对应关系
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,roomSubmitVo.getId()).eq(GraphInfo::getItemType,ItemType.ROOM));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,roomSubmitVo.getId()));
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,roomSubmitVo.getId()));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, roomSubmitVo.getId()));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,roomSubmitVo.getId()));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId()));
        }
        // save
        // 1. save roomInfo --> return 自增Id
        roomInfoService.saveOrUpdate(roomSubmitVo);
        // 2. save graphInfo
        roomSubmitVo.getGraphVoList().forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            BeanUtils.copyProperties(graphVo,graphInfo);
            graphInfo.setItemId(roomSubmitVo.getId());
            graphInfo.setItemType(ItemType.ROOM);
            graphInfoService.saveOrUpdate(graphInfo);
        });
        // 3. save 房间对应关系
        // 3.1  save RoomAttrValue
/*        roomSubmitVo.getAttrValueIds().forEach(aid ->{
            RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(aid).build();
            roomAttrValueService.saveOrUpdate(roomAttrValue);
        });*/
        List<RoomAttrValue> roomAttrValues = roomSubmitVo.getAttrValueIds().stream().map(aid -> RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(aid).build()).collect(Collectors.toList());
        roomAttrValueService.saveOrUpdateBatch(roomAttrValues);

        // 3.2 save RoomFacility
        List<RoomFacility> roomFacilities = roomSubmitVo.getFacilityInfoIds().stream().map(fid -> RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(fid).build()).collect(Collectors.toList());
        roomFacilityService.saveOrUpdateBatch(roomFacilities);

        // 3.3 save RoomLabel
        List<RoomLabel> roomLabels = roomSubmitVo.getLabelInfoIds().stream().map(lid -> RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(lid).build()).collect(Collectors.toList());
        roomLabelService.saveOrUpdateBatch(roomLabels);

        // 3.4 save RoomPaymentType
        List<RoomPaymentType> roomPaymentTypes = roomSubmitVo.getPaymentTypeIds().stream().map(pid -> RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(pid).build()).collect(Collectors.toList());
        roomPaymentTypeService.saveOrUpdateBatch(roomPaymentTypes);

        // 3.5 save RoomLeaseTerm
        List<RoomLeaseTerm> roomLeaseTerms = roomSubmitVo.getLeaseTermIds().stream().map(leaseTermId -> RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build()).collect(Collectors.toList());
        roomLeaseTermService.saveOrUpdateBatch(roomLeaseTerms);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询房间列表")
    @GetMapping("pageItem")
    public Result<IPage<RoomItemVo>> pageItem(@RequestParam long current, @RequestParam long size, RoomQueryVo queryVo) {
        Page<RoomItemVo> page = Page.of(current, size);
        IPage<RoomItemVo> roomItemVoIPage = roomInfoService.pageRoomItemByQuery(page, queryVo);

        return Result.ok(roomItemVoIPage);
    }

    @Operation(summary = "根据id获取房间详细信息")
    @GetMapping("getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {
        RoomDetailVo roomDetailVo = roomInfoService.getRoomDetailById(id);
        return Result.ok(roomDetailVo);
    }

    @Operation(summary = "根据id删除房间信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        roomInfoService.removeRoomById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改房间发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(Long id, ReleaseStatus status) {
        roomInfoService.update(new LambdaUpdateWrapper<RoomInfo>().set(RoomInfo::getIsRelease,status).eq(RoomInfo::getId,id));
        return Result.ok();
    }

    @GetMapping("listBasicByApartmentId")
    @Operation(summary = "根据公寓id查询房间列表")
    public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {
        return Result.ok(roomInfoService.list(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId,id)));
    }

}


















